layui.define(['element', 'api', 'listener', 'amis', 'utils'], function(exports) {
	"use strict";

	/**
	 * Button component
	 * */
	var MOD_NAME = 'action_executer',
		$ = layui.jquery,
        editor = layui.editor,
		api = layui.api,
        listener = layui.listener,
        amis = layui.amis,
        utils = layui.utils
    ;

	var action_executer = function(opt) {
		this.option = opt;
	};

    /**
	 * Button start loading
	 * */
    action_executer.prototype.exec = function(opt) {

         //debugger
		var option = {
            elem: opt.elem,
            schema: opt.schema,
            data: opt.data
		}

		var expression = option.schema.expression;

		if(expression && option.data){
            if(utils.evalExpression(expression, option.data)){
                //当配置了表达式，则需要成立才执行
                exec(option);
            }
        }else{
            exec(option);
        }


        return new action_executer(option);
	}

	function exec(option){
        // debugger
        var schema = option.schema;
        var data = option.data;
        
        var formatSchema = utils.formatSchema(schema, data);

        var actionType = formatSchema.actionType;
        var args = formatSchema.args;
        var script = formatSchema.script;

        
        if(actionType === 'url'){
            var {url, params, blank} = args;
            //True为打开新窗口
            if(blank){
                window.open(url);
            }else{
                location.href = url;
            }
        }else if(actionType === 'copy'){
            const ele = document.createElement('input');
            document.body.appendChild(ele);
            ele.setAttribute('value',args.url);
            ele.select();
            document.execCommand('copy');
            document.body.removeChild(ele);
        }
    }

    exports(MOD_NAME, new action_executer());
});
